// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import Vuex from 'vuex'
import store from './store' // 状态管理
import router from './router'
import 'babel-polyfill' // 可以将ES6代码转为ES5代码

import axios from 'axios'
import globalVariable from '@/api/global_variable.js'
import bsConfig from '@/api/config.js'
import 'element-ui/lib/theme-chalk/index.css';
import './styles/index.scss'
import {
  Carousel,
  CarouselItem,
  Input,
  Select,
  Option,
  Form,
  Button,
  ButtonGroup,
  FormItem,
  Tooltip,
  Tabs,
  TabPane,
  Collapse,
  CollapseItem,
  Loading,
  Progress,
  Image,
  Switch,
  Checkbox,
  Radio,
  Dialog,
  Timeline,
  TimelineItem,
  Autocomplete,
  Card,
  Tag,
  Pagination,
  Popover,
  Tree,
  Cascader,
  DatePicker,
  Row,
  Col,
  Dropdown,
  DropdownMenu,
  DropdownItem
} from 'element-ui'
import ElementUI from 'element-ui';
import InfiniteScroll from 'vue-infinite-scroll'
import Ctl from '@/utils/Controller'
import Cs from '@/api/cs'
// 可拖拽弹框
import './libs/directives'
import md5 from 'js-md5'

Vue.use(Carousel)
Vue.use(CarouselItem)
Vue.use(Input)
Vue.use(Select)
Vue.use(Option)
Vue.use(Form)
Vue.use(Button)
Vue.use(ButtonGroup)
Vue.use(FormItem)
Vue.use(InfiniteScroll)
Vue.use(Tooltip)
Vue.use(Tabs)
Vue.use(TabPane)
Vue.use(Collapse)
Vue.use(CollapseItem)
Vue.use(Loading)
Vue.use(Progress)
Vue.use(Image)
Vue.use(Switch)
Vue.use(Checkbox)
Vue.use(Radio)
Vue.use(Dialog)
Vue.use(Timeline)
Vue.use(TimelineItem)
Vue.use(Autocomplete)
Vue.use(Card)
Vue.use(Tag)
Vue.use(Pagination)
Vue.use(Popover)
Vue.use(Tree)
Vue.use(Cascader)
Vue.use(DatePicker)
Vue.use(Row)
Vue.use(Col)
Vue.use(Dropdown)
Vue.use(DropdownMenu)
Vue.use(DropdownItem)
// 全局变量
Vue.prototype.$axios = axios
Vue.prototype.GLOBAL = globalVariable
Vue.prototype.bsConfig = bsConfig
Vue.prototype.$Ctl = Ctl
Vue.prototype.$Cs = Cs
Vue.prototype.$md5 = md5
Vue.use(Vuex)
ElementUI.Dialog.props.closeOnClickModal.default = false;
// Vue.use(ElementUI)

router.beforeEach((to, from, next) => {
  let _platCode = localStorage.getItem('ilinkBs_platformCode')
  if (!ZHXF_IS_AIO && to.meta.title && typeof (metaTitle) == 'undefined') {
    document.title = to.meta.title
  }
  if ((to.path !== '/login')) { // 不是登录页面
    // 获取分平台个性化配置
    if (process.env.NODE_ENV === 'development') {
      // 开发环境
      if (typeof ZHXF_MAP_TYPE != 'undefined') {
        Vue.prototype.bsConfig.MapOptions.svgMap = ZHXF_MAP_TYPE == 'plan'
      }
    } else {
      // 生产环境
      // 获取分平台个性化配置
      $.ajaxSettings.async = false
      $.getJSON(REMOTESTATICPATH + _platCode + ('/bs-6.0/config' + (['dev', 'test'].indexOf(CONFIGKEY) > -1 ? ('_' + CONFIGKEY) : '') + '.json'), function (data) {
        for (let key in data) {
          Vue.prototype.bsConfig[key] = data[key]
        }
        if (typeof ZHXF_MAP_TYPE != 'undefined') {
          Vue.prototype.bsConfig.MapOptions.svgMap = ZHXF_MAP_TYPE == 'plan'
        }
      })
      $.ajaxSettings.async = true
    }
    // 前端清空了浏览器缓存，服务端session不一定断开的情况
    store.dispatch('setUserInfo', {
      parames: {
        platformCode: _platCode
      },
      callBack: function () {
        if (store.state.isLogin) {
          localStorage.setItem('ilinkBs_orgIds', store.state.userInfo.organizationIds)
          if (Ctl.isCs(true)) {
            store.dispatch('setOrgList')
          }
          next({
            query: _platCode !== 'yxyljg' ? {
              loginId: _platCode
            } : {}
          })
        } else {
          next({
            path: '/login'
          })
        }
      }
    })
  } else { // 登录页面
    next()
  }
})

// 全局调用方法，供C端调用
window.FunList = {}
window.wsForBs = null

window.alert = function (e) {
  console.log(e)
}

// 禁用f5/选中等
Ctl.banF5andSelected()

window.topVue = new Vue({
  el: '#app',
  router,
  store,
  render: h => h(App),
  data: {
    charts: []
  }
})
